<?php

namespace app\admin\model;

use think\Model;

class Department extends Common
{


    public function getTree($companyId)
    {
        $top = $this->where('pid', '=', 'null')->where('company_id', $companyId)->select();
        foreach ($top as $k => $v) {
            $collection = $this->where(['pid' => $v->id])->select();
            $top[$k]['children'] = $collection;
        }
        return $top;
    }

    public function parentDepartment()
    {
        return $this->hasOne('Department', 'id', 'pid');
    }

    public function company()
    {
        return $this->belongsTo('Company', 'company_id', 'id');
    }
}